package com.campus.client.view;

import com.campus.client.controller.AdminMainController;
import com.campus.client.controller.PasswordChangeController;
import com.campus.client.net.ClientConnector;
import com.campus.common.vo.User;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.IOException;

public class PasswordChangeFrame {

    Button confirmBtn = new Button("确定");
    Button cancelBtn = new Button("取消");
    PasswordField oldPasswordField;
    PasswordField newPasswordField;

    private String username;
    private String password;
    private String role;

    public void setUsername(String username) {this.username=username;}
    public void setPassword(String password) {this.password=password;}
    public void setRole(String role) {this.role=role;}
    public String getUsername() {return username;}
    public String getPassword() {return password;}
    public String getRole() {return role;}

    private PasswordChangeController controller;

    public void start(Stage primaryStage) throws IOException {
        // 创建主VBox容器
        VBox mainVBox = new VBox();
        System.out.println("用户名"+username);
        System.out.println("密码"+password);

        ClientConnector connector = new ClientConnector("172.20.10.2", 8888);
        controller = new PasswordChangeController(this, connector);

        mainVBox.setPrefHeight(400.0);
        mainVBox.setPrefWidth(640.0);

        // 创建AnchorPane
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setMaxHeight(Double.NEGATIVE_INFINITY);
        anchorPane.setMaxWidth(Double.NEGATIVE_INFINITY);
        anchorPane.setPrefHeight(-1.0);
        anchorPane.setPrefWidth(-1.0);
        AnchorPane.setTopAnchor(anchorPane, 0.0);
        AnchorPane.setBottomAnchor(anchorPane, 0.0);
        AnchorPane.setLeftAnchor(anchorPane, 0.0);
        AnchorPane.setRightAnchor(anchorPane, 0.0);

        // 创建灰色背景矩形
        Rectangle bgRect = new Rectangle();
        bgRect.setArcHeight(5.0);
        bgRect.setArcWidth(5.0);
        bgRect.setFill(Color.web("#f2f2f2"));
        bgRect.setHeight(232.0);
        bgRect.setLayoutX(106.0);
        bgRect.setLayoutY(112.0);
        bgRect.setStroke(Color.BLACK);
        bgRect.setStrokeType(javafx.scene.shape.StrokeType.INSIDE);
        bgRect.setWidth(437.0);

        // 创建主内容VBox
        VBox contentVBox = new VBox();
        contentVBox.setAlignment(Pos.CENTER);
        contentVBox.setLayoutX(116.0);
        contentVBox.setLayoutY(119.0);
        contentVBox.setPrefHeight(232.0);
        contentVBox.setPrefWidth(417.0);
        contentVBox.setSpacing(20.0);

        // 创建旧密码HBox
        HBox oldPasswordHBox = new HBox();
        oldPasswordHBox.setAlignment(Pos.CENTER);
        oldPasswordHBox.setPrefHeight(46.0);
        oldPasswordHBox.setPrefWidth(417.0);

        Text oldPasswordLabel = new Text("旧密码：");
        oldPasswordLabel.setStrokeWidth(0.0);
        oldPasswordLabel.setFont(Font.font(30.0));

        oldPasswordField = new PasswordField();
        oldPasswordField.setPrefHeight(34.0);
        oldPasswordField.setPrefWidth(241.0);

        oldPasswordHBox.getChildren().addAll(oldPasswordLabel, oldPasswordField);

        // 创建新密码HBox
        HBox newPasswordHBox = new HBox();
        newPasswordHBox.setAlignment(Pos.CENTER);
        newPasswordHBox.setPrefHeight(37.0);
        newPasswordHBox.setPrefWidth(417.0);

        Text newPasswordLabel = new Text("新密码：");
        newPasswordLabel.setStrokeWidth(0.0);
        newPasswordLabel.setFont(Font.font(30.0));

        newPasswordField = new PasswordField();
        newPasswordField.setPrefHeight(34.0);
        newPasswordField.setPrefWidth(241.0);

        newPasswordHBox.getChildren().addAll(newPasswordLabel, newPasswordField);

        // 创建按钮HBox
        HBox buttonsHBox = new HBox();
        buttonsHBox.setAlignment(Pos.CENTER);
        buttonsHBox.setPrefHeight(56.0);
        buttonsHBox.setPrefWidth(417.0);
        buttonsHBox.setSpacing(60.0);

        // 创建确定按钮
        confirmBtn.setMnemonicParsing(false);
        confirmBtn.setPrefHeight(41.0);
        confirmBtn.setPrefWidth(101.0);
        confirmBtn.setStyle("-fx-background-color: #1E90FF;");
        confirmBtn.setTextFill(Color.WHITE);
        confirmBtn.setFont(Font.font("System", FontWeight.BOLD, 18.0));

        // 创建取消按钮
        cancelBtn.setMnemonicParsing(false);
        cancelBtn.setPrefHeight(41.0);
        cancelBtn.setPrefWidth(101.0);
        cancelBtn.setStyle("-fx-background-color: #4a4a4a;");
        cancelBtn.setTextFill(Color.WHITE);
        cancelBtn.setFont(Font.font("System", FontWeight.BOLD, 18.0));

        buttonsHBox.getChildren().addAll(confirmBtn, cancelBtn);

        // 将组件添加到内容VBox
        contentVBox.getChildren().addAll(oldPasswordHBox, newPasswordHBox, buttonsHBox);

        // 创建标题文本
        Text titleText = new Text("修改密码");
        titleText.setLayoutX(248.0);
        titleText.setLayoutY(63.0);
        titleText.setStrokeWidth(0.0);
        titleText.setFont(Font.font("System", FontWeight.BOLD, 36.0));

        // 将所有组件添加到AnchorPane
        anchorPane.getChildren().addAll(bgRect, contentVBox, titleText);

        // 将AnchorPane添加到主VBox
        mainVBox.getChildren().add(anchorPane);

        // 创建场景并显示
        Scene scene = new Scene(mainVBox);
        primaryStage.setTitle("修改密码");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public String getOldPassword() {return oldPasswordField.getText();}
    public String getNewPassword() {return newPasswordField.getText();}

    public void setConfirmButtonAction (java.util.function.Consumer<javafx.event.ActionEvent> handler) {
        confirmBtn.setOnAction(handler::accept);
    }
    public void setCancelButtonAction (java.util.function.Consumer<javafx.event.ActionEvent> handler) {
        cancelBtn.setOnAction(handler::accept);
    }

    public void showMainFrame() throws IOException {
        // 获取当前登录窗口的Stage
        Stage passwordChangeStage = (Stage) cancelBtn.getScene().getWindow();

        // 创建主界面
        if(role=="管理员"){
            AdminMainFrame adminMainFrame = new AdminMainFrame();
            adminMainFrame.setUser(username,password,"管理员");
            Stage stage = new Stage();
            adminMainFrame.show(stage);
        } else if (role=="教师") {
            TeacherMainFrame teacherMainFrame = new TeacherMainFrame();
            teacherMainFrame.setUser(username,password,"教师");

            Stage stage = new Stage();
            teacherMainFrame.show(stage);
        }else {
            StudentMainFrame studentMainFrame = new StudentMainFrame();
            studentMainFrame.setUser(username,password,"学生");

            Stage stage = new Stage();
            studentMainFrame.show(stage);
        }
        passwordChangeStage.close();
    }

}